Skip to content

Conversation

obenland
Copy link
Member

Fixes #2070.

Proposed changes:

  • Modified get_preferred_username() method in includes/model/class-user.php to detect email-based logins and sanitize them using sanitize_title()
  • Added comprehensive test coverage in tests/includes/model/class-test-user.php to verify proper email username sanitization
  • Ensures ActivityPub handles are properly formatted as @[email protected] instead of malformed @[email protected]@domain.com

Other information:

  • Have you written new tests for your changes, if applicable?

Testing instructions:

Setup

  1. Install and activate the ActivityPub plugin in a WordPress test environment
  2. Install and configure Site Kit with Google authentication (or manually create a user with email-based login)

Test Case 1: Email-based Login from Site Kit

  1. Create a test user via Site Kit Google login, which typically creates a username like [email protected]
  2. Navigate to the user's ActivityPub profile page or use the webfinger endpoint
  3. Expected result: ActivityPub handle should be @[email protected]
  4. Previous buggy behavior: Handle was malformed as @[email protected]@yourdomain.com

Test Case 2: Manual Email-based Username

  1. Create a user with login name [email protected]
  2. Check their ActivityPub handle via webfinger or profile
  3. Expected result: Handle should be @[email protected]

Test Case 3: Normal Username (Regression Test)

  1. Create a user with normal login name normaluser
  2. Check their ActivityPub handle
  3. Expected result: Handle should be @[email protected] (unchanged behavior)

Test Case 4: Run Unit Tests

npm run env-test -- --filter=test_email_username_sanitization

Expected result: All tests should pass

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch

Type

  • Fixed - for any bug fixes

Message

Fix malformed ActivityPub handles for users with email-based logins (e.g., from Site Kit Google authentication)

Sanitize email-based usernames (e.g., from Site Kit Google login) to prevent
malformed ActivityPub handles like @[email protected]@domain.com.

- Modified get_preferred_username() to detect and sanitize email logins
- Added comprehensive test coverage for email username sanitization
- Ensures proper webfinger format without double @ symbols

Fixes #2070.
@Copilot Copilot AI review requested due to automatic review settings August 19, 2025 17:20
@obenland obenland self-assigned this Aug 19, 2025
@obenland obenland requested a review from pfefferle August 19, 2025 17:20
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes malformed ActivityPub handles for users with email-based login names, such as those created through Site Kit Google authentication. The issue occurred when usernames containing "@" symbols resulted in double "@" symbols in ActivityPub handles.

  • Modified get_preferred_username() to sanitize email-based usernames using sanitize_title()
  • Added comprehensive test coverage to verify proper email username sanitization
  • Ensures ActivityPub handles are properly formatted without malformed double "@" symbols

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
includes/model/class-user.php Modified get_preferred_username() to detect and sanitize email-based login names
tests/includes/model/class-test-user.php Added comprehensive test cases for email username sanitization scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Prefixed str_contains with a backslash to ensure the global PHP function is used, preventing potential issues with overridden or namespaced functions.
Fix PHPCS alignment warnings for variable assignments.
@Jiwoon-Kim

This comment was marked as duplicate.

@Jiwoon-Kim
Copy link

Jiwoon-Kim commented Aug 19, 2025

The profile URL is generated as:
https://travel-in-busan.com/author/kimjiwoon75gmail-com/

To maintain consistency with the ActivityPub handle, instead of replacing @ with a hyphen, the @ should be omitted entirely.

👉 Desired format:
@[email protected]


is not very “clean” as a username.

It may be worth considering a redirect mechanism in the future so that such legacy usernames can point to a cleaner, newly chosen identifier.

Of course, this would also require solving the name collision problem (e.g., if two users try to adopt the same “cleaned” username).

👉 In practice, this would mean:

Keep the legacy handle for backwards compatibility.

Allow the user/admin to register a new canonical handle.

Set up a redirect/alias system so that old mentions and followers still resolve correctly.


This should be addressed together with the ability for users to edit their blog profile handle. For example, mechanisms that allow arbitrary manipulation of the handle—such as using the posts page slug as the handle—should be prevented. The goal is to maintain consistency and integrity of ActivityPub handles across posts and profiles.

Use explicit string replacement instead of sanitize_title()
to ensure dots are converted to dashes as expected by tests.
Also use filter_var() for proper email validation.
@Jiwoon-Kim
Copy link

@pfefferle
Copy link
Member

pfefferle commented Aug 25, 2025

@obenland you use a different formatter than core, so it is not possible to run a webfinger lookup. I think core simply removes the @ instead of replacing it with a -.


// Handle cases where login is an email address (e.g., from Site Kit Google login).
if ( \filter_var( $login, FILTER_VALIDATE_EMAIL ) ) {
$login = \str_replace( array( '@', '.' ), '-', $login );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

Suggested change
$login = \str_replace( array( '@', '.' ), '-', $login );
$login = \sanitize_title( $login );

???


// Handle cases where login is an email address (e.g., from Site Kit Google login).
if ( \filter_var( $login, FILTER_VALIDATE_EMAIL ) ) {
$login = \str_replace( array( '@', '.' ), '-', $login );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be even better and should be the same result:

Suggested change
$login = \str_replace( array( '@', '.' ), '-', $login );
$login = \get_the_author_meta( 'user_nicename', $this->_id );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it comes down to the expectations we have towards what makes for an appealing handle. a49da59 moved away from sanitize_title() to transition fediverse handles from usernamegmail-com to username-gmail-com.

The latter might be a bit nicer, but happy to defer if you have another preference. I don't feel strongly about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might look nicer, but it breaks the lookup! If you stick with the custom formatter you would have to also add support to the Users::get_by_* functions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that's helpful!

Updated the logic in User::get_preferred_username() to use the user's nicename when the login is an email address, instead of replacing '@' and '.' with dashes. Adjusted related tests to match the new behavior.
@obenland obenland merged commit 4cc26df into trunk Aug 25, 2025
13 checks passed
@obenland obenland deleted the fix/email-username-sanitization branch August 25, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ActivityPub Handle Format Issue with Google Login
4 participants